﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using Infrastructure;
using OpenAuth.Repository.Domain;
using Microsoft.AspNetCore.Authorization;

namespace OpenAuth.WebApi.Areas.HisApi.Controllers
{
    /// <summary>
    /// 门诊预约
    /// </summary>
    [Route("HisApi/[controller]/[action]")]
    [ApiController]
    public class ReservationController : ControllerBase
    {
        /// <summary>
        /// 全院预约号源查询
        /// </summary>
        private readonly CYYGH014App cyygh014App;
        /// <summary>
        /// 预约出诊科室查询
        /// </summary>
        private readonly CYYGH001App cyygh001App;
        /// <summary>
        /// 预约出诊医生查询
        /// </summary>
        private readonly CYYGH005App cyygh005App;
        /// <summary>
        /// 预约医生查询（科室）（医生）、预约医生信息查询
        /// </summary>
        private readonly CYYGH006App cyygh006App;
        /// <summary>
        /// 预约科室号源查询\预约科室号源信息查询
        /// </summary>
        private readonly CYYGH003App cyygh003App;
        /// <summary>
        /// 预约科室号序查询\预约科室号源号序信息查询
        /// </summary>
        private readonly CYYGH004App cyygh004App;
        /// <summary>
        /// 预约医生号源信息查询（预约医生号源查询）
        /// </summary>
        private readonly CYYGH007App cyygh007App;
        /// <summary>
        /// 预约医生号源号序信息查询（预约医生号序查询）
        /// </summary>
        private readonly CYYGH008App cyygh008App;
        /// <summary>
        /// 预约医生号源号序信息查询（预约医生号序查询）
        /// </summary>
        private readonly CYYGH009App cyygh009App;
        /// <summary>
        /// 爽约信息查询(查询患者爽约信息)
        /// </summary>
        private readonly CYYGH010App cyygh010App;
        /// <summary>
        /// 停诊信息查询(门诊停诊信息查询)
        /// </summary>
        private readonly CYYGH011App cyygh011App;
        /// <summary>
        /// 停诊信息查询_增量查询(门诊停诊查询(增量轮询))
        /// </summary>
        private readonly CYYGH012App cyygh012App;
        /// <summary>
        /// 查询指定排班号源信息(指定排班号源查询)
        /// </summary>
        private readonly CYYGH013App cyygh013App;
        /// <summary>
        /// 门诊预约登记
        /// </summary>
        private readonly BYYGH001App byygh001App;
        public ReservationController(
            CYYGH014App _cyygh014App,
            CYYGH001App _cyygh001App,
            CYYGH005App _cyygh005App,
            CYYGH006App _cyygh006App,
            CYYGH003App _cyygh003App,
            CYYGH004App _cyygh004App,
            CYYGH007App _cyygh007App,
            CYYGH008App _cyygh008App,
            CYYGH009App _cyygh009App,
            CYYGH010App _cyygh010App,
            CYYGH011App _cyygh011App,
            CYYGH012App _cyygh012App,
            CYYGH013App _cyygh013App,
            BYYGH001App _byygh001App
            )
        {
            cyygh014App = _cyygh014App;
            cyygh001App = _cyygh001App;
            cyygh005App = _cyygh005App;
            cyygh006App = _cyygh006App;
            cyygh003App = _cyygh003App;
            cyygh004App = _cyygh004App;
            cyygh007App = _cyygh007App;
            cyygh008App = _cyygh008App;
            cyygh009App = _cyygh009App;
            cyygh010App = _cyygh010App;
            cyygh011App = _cyygh011App;
            cyygh012App = _cyygh012App;
            cyygh013App = _cyygh013App;
            byygh001App = _byygh001App;

        }
        /// <summary>
        /// 全院预约号源查询///测试没问题
        /// </summary>
        /// <param name="ksrq">开始日期</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH014ByKsrq(string ksrq)
        {
            //准备返回对象
            var result = new Response<List<CYYGH014>>();
            var queryResult = cyygh014App.QueryCYYGH014ByKsrq(ksrq);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    qyhys = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约出诊科室查询///测试没问题
        /// </summary>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH001ByKsrq(string ksrq, string jsrq)
        {
            //准备返回对象
            var result = new Response<List<CYYGH001>>();
            var queryResult = cyygh001App.QueryCYYGH001ByKsrq(ksrq, jsrq);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    czksxxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约出诊医生查询///测试没问题
        /// </summary>
        /// <param name="ksdm">科室代码</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH005ByKsdm(string ksdm, string ksrq, string jsrq)
        {
            //准备返回对象
            var result = new Response<List<CYYGH005>>();
            var queryResult = cyygh005App.QueryCYYGH005ByKsdm(ksdm, ksrq, jsrq);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    czysxxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约医生查询（科室）、预约医生信息查询///测试没问题
        /// </summary>
        /// <param name="ksdm">科室代码</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <param name="czlx">出诊类型</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH006ByKsdm(string ksdm, string ksrq, string jsrq, string czlx)
        {
            //准备返回对象
            var result = new Response<List<CYYGH006>>();
            var queryResult = cyygh006App.QueryCYYGH006ByKsdm(ksdm, ksrq, jsrq, czlx);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yyyss = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约医生查询（医生）、预约医生信息查询///测试没问题
        /// </summary>
        /// <param name="ksrq">开始日期</param>k
        /// <param name="jsrq">结束日期</param>
        /// <param name="czlx">出诊类型</param>
        /// 
        /// <param name="ysdm">医生代码</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH006ByYsdm(string ksrq, string jsrq, string czlx, string ysdm)
        {
            //准备返回对象
            var result = new Response<List<CYYGH006>>();
            var queryResult = cyygh006App.QueryCYYGH006ByYsdm(ksrq, jsrq, czlx, ysdm);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yyyss = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约科室号源查询\预约科室号源信息查询///测试没问题
        /// </summary>
        /// <param name="ksdm">科室代码</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <param name="zzlx">坐诊时间类型</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH003ByKsdm(string ksdm, string ksrq, string jsrq, string zzlx)
        {
            //准备返回对象
            var result = new Response<List<CYYGH003>>();
            var queryResult = cyygh003App.QueryCYYGH003ByKsdm(ksdm, ksrq, jsrq, zzlx);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yykshyhxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约科室号序查询\预约科室号源号序信息查询///测试没问题
        /// </summary>
        /// <param name="pbxh">排班序号</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH004ByPbxh(string pbxh)
        {
            //准备返回对象
            var result = new Response<List<CYYGH004>>();
            var queryResult = cyygh004App.QueryCYYGH004ByPbxh(pbxh);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yykshyhxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约医生号源信息查询（预约医生号源查询）///测试没问题
        /// </summary>
        /// <param name="ysdm">医生代码</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <param name="zzlx">坐诊时间类型</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH007ByYsdm(string ysdm, string ksrq, string jsrq, string zzlx)
        {
            //准备返回对象
            var result = new Response<List<CYYGH007>>();
            var queryResult = cyygh007App.QueryCYYGH007ByYsdm(ysdm, ksrq, jsrq, zzlx);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yyyshys = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约医生号源号序信息查询（预约医生号序查询）///测试没问题
        /// </summary>
        /// <param name="pbxh">排班序号</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH008ByPbxh(string pbxh)
        {
            //准备返回对象
            var result = new Response<List<CYYGH008>>();
            var queryResult = cyygh008App.QueryCYYGH008ByPbxh(pbxh);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yyyshyhxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约信息查询（查询患者预约信息）///测试没问题
        /// </summary>
        /// <param name="patid">患者唯一编码</param>
        /// <param name="rqlx">日期类型</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <param name="hzxm">患者姓名</param>
        /// <param name="czybz">操作员标志</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH009ByPatid(string patid, string rqlx, string ksrq, string jsrq, string hzxm, string czybz)
        {
            //准备返回对象
            var result = new Response<List<CYYGH009>>();
            var queryResult = cyygh009App.QueryCYYGH009ByPatid(patid, rqlx, ksrq, jsrq, hzxm, czybz);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    mzyyxxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 预约信息查询（查询患者预约信息）
        /// </summary>
        /// <param name="yyxh">预约序号</param>
        /// <param name="hzxm"></param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH009ByYyxh(string yyxh, string hzxm)
        {
            //准备返回对象
            var result = new Response<List<CYYGH009>>();
            var queryResult = cyygh009App.QueryCYYGH009ByYyxh(yyxh, hzxm);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    mzyyxxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }).ToJson();
            }
        }
        /// <summary>
        /// 爽约信息查询(查询患者爽约信息)///没测出来
        /// </summary>
        /// <param name="patid">患者唯一编码</param>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <param name="hzxm">患者姓名</param>
        /// <param name="czybz">操作员标志</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH010ByPatid(string patid, string ksrq, string jsrq, string hzxm, string czybz)
        {
            //准备返回对象
            var result = new Response<List<CYYGH010>>();
            var queryResult = cyygh010App.QueryCYYGH010ByPatid(patid, ksrq, jsrq, hzxm, czybz);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    mzyyxxs = result.Result

                }.ToJson();
            }
            else
            {
                return (new { success = "false", requestId = Guid.NewGuid().ToString() }).ToJson();
            }
        }
        /// <summary>
        /// 停诊信息查询(门诊停诊信息查询)///测试没问题
        /// </summary>
        /// <param name="ksrq">开始日期</param>
        /// <param name="jsrq">结束日期</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH011ByKsrq(string ksrq, string jsrq)
        {
            //准备返回对象
            var result = new Response<List<CYYGH011>>();
            var queryResult = cyygh011App.QueryCYYGH011ByKsrq(ksrq, jsrq);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    tzxxs = result.Result

                }.ToJson();
            }
            else
            {
                //
                return (new { success = "false", requestId = Guid.NewGuid().ToString() }).ToJson();
            }
        }
        /// <summary>
        /// 停诊信息查询_增量查询(门诊停诊查询(增量轮询))///测试没问题
        /// </summary>
        /// <param name="lastmaxczsjc">最后一次操作开始时间戳</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH012ByLastmaxczsjc(string lastmaxczsjc)
        {
            //准备返回对象
            var result = new Response<List<CYYGH012>>();
            var queryResult = cyygh012App.QueryCYYGH012ByLastmaxczsjc(lastmaxczsjc);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    tzxxs = result.Result.Select((p) => new { p.czsjc, p.pbxh })

                }.ToJson();
            }
            else
            {
                //
                return (new { success = "false", requestId = Guid.NewGuid().ToString() }).ToJson();
            }
        }
        /// <summary>
        /// 查询指定排班号源信息(指定排班号源查询)///测试没问题
        /// </summary>
        /// <param name="pbxh">排班序号</param>
        /// <returns></returns>
        [HttpGet]
        public string QueryCYYGH013ByPbxh(string pbxh)
        {
            //准备返回对象
            var result = new Response<List<CYYGH013>>();
            var queryResult = cyygh013App.QueryCYYGH013ByPbxh(pbxh);
            result.Result = queryResult.ToList();
            if (result.Result.Count > 0)
            {
                return new
                {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    tzxxs = result.Result.Select((p) => new { p.czlx, p.pbxh })

                }.ToJson();
            }
            else
            {
                //
                return (new { success = "false", requestId = Guid.NewGuid().ToString() }).ToJson();
            }
        }
        /// <summary>
        /// 门诊预约登记
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public string PutBYYGH001([FromBody]BYYGH001 model)
        {
            var result = byygh001App.PutBYYGH001(model).First();
            
            if (result.Code == 200)
            {

                return (new {
                    success = "true",
                    requestId = Guid.NewGuid().ToString(),
                    yyxh = result.yyxh.ToString(),
                    ksdm = result.ksdm.ToString(),
                    ksmc = result.ksmc.ToString(),
                    ysdm = result.ysdm.ToString(),
                    ysmc = result.ysmc.ToString(),
                    ghrq = result.ghrq.ToString(),
                    yyhx = result.yyhx.ToString(),
                    ghf = result.ghf.ToString(),
                    zlf = result.zlf.ToString(),
                    ysje = result.ysje.ToString(),
                    zje = result.zje.ToString(),
                    memo = result.memo.ToString(),
                    yysjd = result.yysjd.ToString(),
                    yysd = result.yysd.ToString(),
                    jzdz = result.jzdz.ToString(),
                    yysj = result.yysj.ToString(),
                }).ToJson();
            }
            else
            {
                return new { success = "false", requestId = Guid.NewGuid().ToString(), message = result.Message }.ToJson();

            }
        }
    }
}